package protocbuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import protocbuf.protoc.DataInfo;

import java.util.Random;

public class ProtocClientHandler extends SimpleChannelInboundHandler<DataInfo.Student> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, DataInfo.Student student) throws Exception {
        System.out.println(student);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        boolean b = new Random().nextBoolean();

        if (b){
            DataInfo.Student student = DataInfo.Student.newBuilder()
                    .setName("Jack")
                    .setAge(18)
                    .setAddress("Austria")
                    .build();
            DataInfo.MyMessage.Builder message = DataInfo.MyMessage.newBuilder().setDataType(DataInfo.MyMessage.DataType.StudentType).setStudent(student);

            ctx.channel().writeAndFlush(message);
        }else {
            DataInfo.Teacher teacher = DataInfo.Teacher.newBuilder()
                    .setName("teacher ma")
                    .setAge(33)
                    .setClass_("class1")
                    .build();

            DataInfo.MyMessage.Builder message = DataInfo.MyMessage.newBuilder().setDataType(DataInfo.MyMessage.DataType.TeacherType).setTeacher(teacher);

            ctx.channel().writeAndFlush(message);
        }


    }
}
